package com.yaodian100.ec.activity.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.yaodian100.core.common.entity.BaseEntity;

/**
 * 
 * @author chin.hung
 *
 */
@Entity
@Table(name = "EC_ACTIVITY_BLOCK_TEMPLATE")
public class ActivityPageBlockTemplate extends BaseEntity {
	private static final long serialVersionUID = 1L;

	/**
	 * 流水號
	 */
	@Id
	@Column(name = "ID")
	private Long id;
	
	@Column(name = "NAME")
	private String name;
	
	@Column(name = "PROD_IMG_TYPE")
	private String productImageType;
	
	@Column(name = "TEMPLATES")
	private String templates;

	@Column(name = "ICON_IMAGE_FILE_PATH")
	private String iconImageFilePath;
	
	@Column(name = "BIG_IMAGE_COUNT")
	private Integer bigImageCount;
	
	@Column(name = "SMALL_IMAGE_COUNT")
	private Integer smallImageCount;
	
	@Column(name = "DIV_CLASS")
	private String divClass;
	
	@Transient
	private ActivityTemplateFormat templateFormat;
	
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProductImageType() {
		return this.productImageType;
	}

	public void setProductImageType(String productImageType) {
		this.productImageType = productImageType;
	}

	public String getTemplates() {
		return templates;
	}

	public void setTemplates(String templates) {
		this.templates = templates;
	}

	public String getIconImageFilePath() {
		return this.iconImageFilePath;
	}

	public void setIconImageFilePath(String iconImageFilePath) {
		this.iconImageFilePath = iconImageFilePath;
	}

	
	public Integer getBigImageCount() {
		return bigImageCount;
	}

	public void setBigImageCount(Integer bigImageCount) {
		this.bigImageCount = bigImageCount;
	}

	public Integer getSmallImageCount() {
		return smallImageCount;
	}

	public void setSmallImageCount(Integer smallImageCount) {
		this.smallImageCount = smallImageCount;
	}

	public String getDivClass() {
		return this.divClass;
	}

	public void setDivClass(String divClass) {
		this.divClass = divClass;
	}
	
	public int getImageWidth(int index) {
		return this.getImageSize(index, "width");
	}
	public int getImageHeight(int index) {
		return this.getImageSize(index, "height");
	}
	public int getImageLimit(int index) {
		return this.getImageSize(index, "limit");
	}
	private int getImageSize(int index,String flag) {
		getTemplateFormat();
		try {
			if (flag.equals("width")) {
				return this.templateFormat.getImageWidth(index);
			} else if (flag.equals("height")) {
				return this.templateFormat.getImageHeight(index);
			} else if (flag.equals("limit")) {
				return this.templateFormat.getFileSizeLimit(index);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	private class ProductTemplateFormat implements ActivityTemplateFormat {
		private Map<String,String[]> imageSize = new HashMap<String,String[]>();
		public ProductTemplateFormat(String format) {
			String[] imageTemplates = format.split(",");
			for (int i=0;i<imageTemplates.length;i++) {
				String template = imageTemplates[i];
				String[] img = template.split("x");
				imageSize.put(String.valueOf(i), img);
			}
		}
		@Override
		public int getImageWidth(int index) {
			return Integer.parseInt(imageSize.get(String.valueOf(index))[0]);
		}
		@Override
		public int getImageHeight(int index) {
			return Integer.parseInt(imageSize.get(String.valueOf(index))[1]);
		}
		@Override
		public int getFileSizeLimit(int index) {
			return 0;
		}
	}
	
	public class BannerTemplateFormat implements ActivityTemplateFormat {
		private List<String[]> imageSize = new ArrayList<String[]>();
		private List<Integer> fileLimit = new ArrayList<Integer>();
		public BannerTemplateFormat(String format) {
			String[] imageTemplates = format.split(",");
			for (int i=0;i<imageTemplates.length;i++) {
				String template = imageTemplates[i];
				String[] data = template.split(";");
				String[] img = data[0].split("x");
				imageSize.add(img);
				fileLimit.add(new Integer(data[1]));
			}
		}
		@Override
		public int getImageWidth(int index) {
			return Integer.parseInt(imageSize.get(index)[0]);
		}
		@Override
		public int getImageHeight(int index) {
			return Integer.parseInt(imageSize.get(index)[1]);
		}
		@Override
		public int getFileSizeLimit(int index) {
			return fileLimit.get(index);
		}
		public List<String[]> getImageSize() {
			return imageSize;
		}
		public List<Integer> getFileLimit() {
			return fileLimit;
		}
	}
	
	private ActivityTemplateFormat getTemplateFormat(String prodImgType,String templates) {
		switch (prodImgType.charAt(0)) {
		case '1': case '2': //product
			return new ProductTemplateFormat(templates);
		case '3': //banner
			return new BannerTemplateFormat(templates);
		default:
			return null;
		}
	}

	public ActivityTemplateFormat getTemplateFormat() {
		if (this.templateFormat == null) this.templateFormat = this.getTemplateFormat(this.productImageType, this.templates);
		return templateFormat;
	}

	public void setTemplateFormat(ActivityTemplateFormat templateFormat) {
		this.templateFormat = templateFormat;
	}
	
	
}
